package sound;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import android.content.Context;
import android.media.MediaPlayer;

public class SoundPlayer {
	MediaPlayer mediaPlayer;
	Context context;
	FileInputStream fis;
	public SoundPlayer(Context context) {
		mediaPlayer = new MediaPlayer();
		this.context = context;
	}
	
	public void play(String fileName) {
		try {
            File file = new File (fileName);
			fis = new FileInputStream(file);
        	mediaPlayer.reset();
			mediaPlayer.setDataSource(fis.getFD());
	    	mediaPlayer.setLooping(false);
	    	mediaPlayer.setVolume(1.0f, 1.0f);
	    	mediaPlayer.prepare();
	    	mediaPlayer.start();
	    	while (mediaPlayer.isPlaying()) {}
	    	fis.close();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public void stop() {
		mediaPlayer.stop();
	}
}
